package cn.mumzone.pos.ui.widgets;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.CardBalanceEntity;
import cn.mumzone.pos.util.AppUtil;

/**
 * author:        yupeng
 * created on:    2019/7/11 15:11
 * description:   储值、次卡汇总
 */
public class CardBalanceView extends LinearLayout {

    LinearLayout itemLayout;
    View viewLine;
    TextView tvTitle;
    TextView tvCount;
    TextView tvAmount;
    TextView tvWxPayAmount;
    TextView tvZfbPayAmount;
    TextView tvCardPayAmount;
    TextView tvCashPayAmount;

    private Context context;

    public CardBalanceView(Context context) {
        this(context, null);
    }

    public CardBalanceView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public CardBalanceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(VERTICAL);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.item_card_balance_list, this);
        itemLayout = findViewById(R.id.item_layout);
        viewLine = findViewById(R.id.view_line);
        tvTitle = findViewById(R.id.tv_title);
        tvCount = findViewById(R.id.tv_count);
        tvAmount = findViewById(R.id.tv_amount);
        tvWxPayAmount = findViewById(R.id.tv_wx_pay_amount);
        tvZfbPayAmount = findViewById(R.id.tv_zfb_pay_amount);
        tvCardPayAmount = findViewById(R.id.tv_card_pay_amount);
        tvCashPayAmount = findViewById(R.id.tv_cash_pay_amount);
    }

    public void bindData(CardBalanceEntity entity, boolean isLastItem) {
        int titleColor = context.getResources().getColor(android.R.color.white);
        int normalColor = context.getResources().getColor(R.color.title_text_color);
        int zeroColor = context.getResources().getColor(R.color.content_text_color);

        if (entity == null) { // 标题栏：黑色背景，白色文字18sp
            tvTitle.setText("");
            tvCount.setText("数量");
            tvAmount.setText("总额");
            tvWxPayAmount.setText("微信支付");
            tvZfbPayAmount.setText("支付宝支付");
            tvCardPayAmount.setText("银行卡支付");
            tvCashPayAmount.setText("现金支付");
            itemLayout.setBackgroundColor(context.getResources().getColor(R.color.toast_guide_level_one));
            tvTitle.setTextColor(titleColor);
            tvCount.setTextColor(titleColor);
            tvAmount.setTextColor(titleColor);
            tvWxPayAmount.setTextColor(titleColor);
            tvZfbPayAmount.setTextColor(titleColor);
            tvCardPayAmount.setTextColor(titleColor);
            tvCashPayAmount.setTextColor(titleColor);
            tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            tvCount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            tvAmount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            tvWxPayAmount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            tvZfbPayAmount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            tvCardPayAmount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            tvCashPayAmount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        } else { // 内容
            tvTitle.setText("1".equals(entity.getType()) ? "计次卡售卖" : "2".equals(entity.getType()) ? "计次卡退卡" : "会员余额充值");
            tvCount.setText(String.valueOf(StringUtils.getInt(entity.getCount())));
            tvAmount.setText(StringUtils.getFormattedMoneyWithUnit(entity.getAmount()));
            tvWxPayAmount.setText(StringUtils.getFormattedMoneyWithUnit(entity.getWxPayAmount()));
            tvZfbPayAmount.setText(StringUtils.getFormattedMoneyWithUnit(entity.getZfbPayAmount()));
            if (!AppUtil.isAppSunMiVersion() && ("1".equals(entity.getType()) || "2".equals(entity.getType()))) {
                tvCardPayAmount.setText("---");
            } else {
                tvCardPayAmount.setText(StringUtils.getFormattedMoneyWithUnit(entity.getCardPayAmount()));
            }

            tvCashPayAmount.setText(StringUtils.getFormattedMoneyWithUnit(entity.getCashPayAmount()));
            itemLayout.setBackgroundColor(context.getResources().getColor(android.R.color.white));
            tvTitle.setTextColor(normalColor);
            tvCount.setTextColor(StringUtils.getDouble(entity.getCount()) == 0 ? zeroColor : normalColor);
            tvAmount.setTextColor(StringUtils.getDouble(entity.getAmount()) == 0 ? zeroColor : normalColor);
            tvWxPayAmount.setTextColor(StringUtils.getDouble(entity.getWxPayAmount()) == 0 ? zeroColor : normalColor);
            tvZfbPayAmount.setTextColor(StringUtils.getDouble(entity.getZfbPayAmount()) == 0 ? zeroColor : normalColor);
            tvCardPayAmount.setTextColor(StringUtils.getDouble(entity.getCardPayAmount()) == 0 ? zeroColor : normalColor);
            tvCashPayAmount.setTextColor(StringUtils.getDouble(entity.getCashPayAmount()) == 0 ? zeroColor : normalColor);
        }

        viewLine.setVisibility(isLastItem ? View.GONE : View.VISIBLE);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }
}
